using UnityEngine;
using System.Collections;

/***********************
 * HealthManager.cs
 * 
 * Manages health for destructable GameObjects
 * ********************/

public class HealthManager : MonoBehaviour {
	
	public int health;
	public float invulTimeLimit; // The total time an GameObject becomes invulnerable for upon taking damage
	private float invulTimer; // The remaining time for which this GameObject is invulnerable

	// Use this for initialization
	void Start () {
		
		if(health == 0) {
			health = 1;
		}
	
		invulTimer = 0.0f;
	}
	
	// Update is called once per frame
	void Update () {
		
		if(invulTimer > 0.0f) {
			invulTimer -= Time.deltaTime;
		}
			
	
	}
	
	void OnTriggerEnter(Collider thisCollider) {
		
		Hazard thisHazard;
		
		thisHazard = thisCollider.gameObject.GetComponent<Hazard>();
		
		
		
		if (thisHazard != null && invulTimer <= 0.0f) {
			health -= (int) thisHazard.damage;
            if (gameObject.GetComponent<RobotController>())
			    gameObject.GetComponent<RobotController>().setPanic();
            if (gameObject.GetComponent<DestructibleCrate>())
                gameObject.GetComponent<DestructibleCrate>().Damaged();
            invulTimer = invulTimeLimit;
		}
		
		if (health <= 0) {
            if (gameObject.GetComponent<RobotController>())
                gameObject.GetComponent<RobotController>().Die();
            if (gameObject.GetComponent<DestructibleCrate>())
                gameObject.GetComponent<DestructibleCrate>().Die();
		}
			
	}
	
	public bool isInvul() {
		if (invulTimer > 0.0f ) {
			return true;
		} 
		else {
			return false;
		}
	}
}
